package com.nulldev.util.net.experimental.udp2.demo;

import java.util.UUID;

import com.nulldev.util.net.IPUtil;
import com.nulldev.util.net.experimental.udp2.Client;
import com.nulldev.util.net.experimental.udp2.Connection;
import com.nulldev.util.net.experimental.udp2.DataReceivedListener;
import com.nulldev.util.net.experimental.udp2.Message;
import com.nulldev.util.net.experimental.udp2.Server;
import com.nulldev.util.net.experimental.udp2.util.SerializationUtils;

public class UDP2Demo {

	public static void main(String[] args) {
		final int port = IPUtil.nextFreePort();
		final Server s = new Server(port, "localhost", 32);
		s.startServer();
		s.registerServerListerner(new DataReceivedListener() {
			
			@Override
			public void receivedMessage(Message message, final Connection connection) {
				System.out.println(message);
				System.out.println("Message.contents=" + message.read());
			}
			
			@Override
			public void disconnected(UUID connectionId) {
			}
			
			@Override
			public void connected(Connection connection) {
				// TODO Auto-generated method stub
				
			}
		});
		final Client c = new Client(port, "localhost");
		c.startClient();
		for (int i = 0; i < 10; i++) {
			c.sendReliableDataObject("Hello world!");
		}
	}

}
